package org.inspursc.s0517.health.evaluation.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.inspursc.s0517.health.evaluation.dto.CategoryDTO;
import org.inspursc.s0517.health.evaluation.dto.QuestionCategoryDTO;
import org.inspursc.s0517.health.evaluation.dto.QuestionDTO;
import org.inspursc.s0517.health.evaluation.service.QuestionCategoryService;
import org.inspursc.s0517.health.common.util.ResponseUtil;
import org.inspursc.s0517.health.common.vo.ResponseVO;
import org.inspursc.s0517.health.evaluation.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author B_Dream
 * @version 2018/5/18 15:15
 */
@RestController
@RequestMapping(value = "/question")
@Api(value = "问题API", description = "问题API")
public class QuestionController {


    @Autowired
    QuestionCategoryService categoryService;


    @Autowired
    QuestionService questionService;


    @ApiOperation("增加/修改分类数据")
    @RequestMapping(value = "/saveCategory", method = RequestMethod.POST)
    public ResponseEntity<ResponseVO> saveCategory(@RequestBody @Valid CategoryDTO category, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
//            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
            return ResponseUtil.error400(bindingResult.getFieldError().getDefaultMessage(), false);

        }
        CategoryDTO categoryDTO = categoryService.saveCategory(category);
        if (categoryDTO != null) {
            return ResponseUtil.success("保存成功！", categoryDTO);
        } else {
            return ResponseUtil.error("保存失败！", false);
        }
    }

    @ApiOperation("删除分类数据")
    @RequestMapping(value = "/deleteCategory", method = RequestMethod.DELETE)
    public ResponseEntity<ResponseVO> deleteCategory(@ApiParam("分类Id") @RequestParam(value = "id") Integer id) {
        Boolean is = categoryService.deleteCategory(id);
        if (is) {
            return ResponseUtil.success("删除成功！", true);
        } else {
            return ResponseUtil.error("删除失败！", false);
        }
    }

    @ApiOperation("获取全部分类数据")
    @RequestMapping(value = "/listCategory", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> listCategory() {
        List<QuestionCategoryDTO> categorys = categoryService.findAll();
        if (categorys.isEmpty() || categorys.size() == 0) {
            return ResponseUtil.error404("无数据！", categorys);
        } else {
            return ResponseUtil.success("获取成功！", categorys);
        }
    }

    @ApiOperation("获取全部分类数据包含问题")
    @RequestMapping(value = "/listCategoryAndQuestion", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> listCategoryAndQuestion(@RequestParam(defaultValue = "1")int typeId) {
       // List<QuestionCategoryDTO> categorys = categoryService.findAllAndQustion();
        List<QuestionCategoryDTO> categorys = categoryService.findQuestionByTypeId(typeId);
        if (categorys.isEmpty() || categorys.size() == 0) {
           // return ResponseUtil.error404("服务器没有该数据！", categorys);
            return ResponseUtil.success("未查到数据！", categorys);
        } else {
            return ResponseUtil.success("获取成功！", categorys);
        }
    }

    @ApiOperation("获取某一分类下所有试题")
    @RequestMapping(value = "/listQuestion", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> listQuestion(@ApiParam("分类Id") @RequestParam Integer categoryId) {
        List<QuestionDTO> dtos = questionService.listQuestions(categoryId);

        if (dtos.isEmpty()) {
            return ResponseUtil.error404("无数据！", null);
        } else {
            return ResponseUtil.success("查询成功！", dtos);
        }

    }


    @ApiOperation("获取单个分类数据")
    @RequestMapping(value = "/getCategory", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> getCategory(@ApiParam("分类父Id") @RequestParam(value = "parentId") Integer parentId) {

        List<QuestionCategoryDTO> dtos;
        try {
            dtos = categoryService.getCategory(parentId);
            return ResponseUtil.success(dtos);
        }catch (Exception e){
            return ResponseUtil.error(e.getMessage(),null);
        }
    }


    @ApiOperation("新增/修改问题")
    @RequestMapping(value = "/saveQuestion", method = RequestMethod.POST)
    public ResponseEntity<ResponseVO> saveQuestion(@ApiParam("问题及选项信息") @RequestBody @Valid QuestionDTO questionDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
//            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
            return ResponseUtil.error400(bindingResult.getFieldError().getDefaultMessage(), false);
        }
        try {
            QuestionDTO dto = questionService.saveQuestion(questionDTO);
            return ResponseUtil.success(dto);
        } catch (Exception e) {
            return ResponseUtil.error(e.getMessage(),null);
        }

    }


    @ApiOperation("根据问题Id，删除问题")
    @RequestMapping(value = "/deleteQuestion", method = RequestMethod.DELETE)
    public ResponseEntity<ResponseVO> deleteQuestion(@ApiParam("题目Id") @RequestParam Integer id) {

        try {
            Boolean is = questionService.deletesById(id);
            if (is) {
                return ResponseUtil.success("删除成功！", true);
            } else {
                return ResponseUtil.error("删除失败！", false);
            }
        } catch (Exception e) {
            return ResponseUtil.error(e.getMessage(), false);
        }
    }


}
